﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGSVR.Common;
using System.Data.SqlClient;
using System.Data;

namespace LGSVR.BLL.Terminal
{
    public class CardGroupBLL
    {
       
        public DataTable GetAllGroup(int id,string userID)
        {
            string sql = "select a.* from cardGroup a ";
            if (userID != "")
            {
                sql += " inner join userCardGroup b on b.CardGroupID=a.groupID and b.state=1 and b.userid='" + userID + "'";
            }
          
            sql += " where a.state=1 ";
            if (id != 0)
            {
                sql += " and a.id!=@ID";
            }
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@ID", id));
            DataTable dt = SQLHelper.GetDataTable(sql, CommandType.Text, lstParam.ToArray());
            dt.Columns.Add("NewGroupName");
            DataTable newDt = dt.Clone();
           
            ConvertDt("", dt, newDt, 0);
            return newDt;
        }
        private DataTable ConvertDt(string parentID, DataTable dt, DataTable newDt, int level)
        {
            int Plot = 1;
            string strWhere = string.Empty;
            if (parentID == "")
            {
                strWhere = "parentID is null or parentID=''";
            }
            else
            {
                strWhere = "parentID='" + parentID + "'";
            }
            level++;
            foreach (DataRow dr in dt.Select(strWhere))
            {
                Plot = level * 2;
                int sLen = (int)dr["groupName"].ToString().Length;
                dr["NewGroupName"] = dr["groupName"].ToString().PadLeft(Plot + sLen, '　');
                newDt.ImportRow(dr);
                ConvertDt(dr["groupID"].ToString(), dt, newDt, level);
            }
            return newDt;
        }
        public bool IsHasCardGroupID(string groupID, int id)
        {
            string sql = "select count(*) from cardGroup where state=1 and [groupID]=@GroupID";
            if (id != 0)
            {
                sql += " and id!=@ID";
            }
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@GroupID", groupID));
            lstParam.Add(new SqlParameter("@ID", id));

            int len = Convert.ToInt32(SQLHelper.GetScale(sql, System.Data.CommandType.Text, lstParam.ToArray()));
            return len > 0;
        }

        ///// <summary>
        ///// 获得
        ///// </summary>
        ///// <param name="userID"></param>
        ///// <returns></returns>
        //public DataTable GetUnableList(string userID)
        //{
        //    string str = "select * from cardGroup ";
        //}
    }
}
